<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 11:27
 */
namespace app\admin\controller;
use think\Db;
class Member extends Base {
    /***
     * @return mixed
     * 首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['uf.factory_name|up.p_username'] = ['like',"%" . $key . "%"];
            }
            if(isset($key)&&$key!="")
            {
                $map['uf.factory_name|up.p_username'] = ['like',"%" . $key . "%"];
            }
            //厂家名称
            if(isset($fid)&&$fid!="")
            {
                $map['uf.factory_id'] = $fid;
            }
            //省级管理员名称
            if(isset($pid)&&$pid!="")
            {
                $map['up.p_id'] = $pid;
            }
            //时间筛选
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.m_create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.m_create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.m_create_time'] = ['between time',[$start,$end]];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_factory_member')->alias('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_factory_member')
                ->alias ('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->page($page, $limit)
                ->where($map)
                ->field('a.*,up.p_username,uf.factory_name')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $province_info = Db::table('up_province')->field('p_username,p_id')->select();
        $factory_info = Db::table('up_factory')->field('factory_id,factory_name')->select();
        $this->assign('province_info',$province_info);
        $this->assign('factory_info',$factory_info);
       return $this->fetch();
    }

    /****
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_factory_member')->where('m_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    public function add(){
        if(request()->isPost()){
            $len = mb_strlen(input('param.m_name'),'utf8');
            $m_serial = mb_strlen(input('param.m_serial'),'utf8');
            if($len>5){
                return json(['code' => 100, 'data' => '', 'msg' => '不能超过5个字符']);
            }
            if($m_serial>30){
                return json(['code' => 100, 'data' => '', 'msg' => '用户编号超过5个字符']);
            }
            $m_id = Db::table('up_factory_member')->where('m_serial',input('param.m_serial'))->value('m_id');
            if($m_id){
                return json(['code' => 100, 'data' => '', 'msg' => '不能添加相同的用户编号']);
            }

            $data['m_serial'] = input('param.m_serial');
            $data['m_name'] = input('param.m_name');
            $data['m_factory_id'] = input('param.m_factory_id');
            $data['m_create_time'] = date('Y-m-d H:i:s',time());
            $res =  Db::table('up_factory_member')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data1 = Db::table('up_factory')->field('factory_id,factory_name')->select();
        $this->assign('data1',$data1);
        return $this->fetch();
    }
}